/* 
 * This file contains the jQuery ajax calls functions
 */

/**
 * These variable store the parameters for executing the query in function sendRequestToServer
 */
var url="";
var jsonQueryData;
//var jsonResponseData;

var callbackSuccess;
var callbackError;

//tmp vars
var tagData;



/**
 * Makes the actual call to the markMyWeb server 
 * METHOD: POST
 * FORMAT: JSON
 */
function sendRequestToServer(callback) {
    $.post(
        //$.getJSON(
        url,            
        jsonQueryData
        )
    .success(function(data) {
        currentConsole.log("jsonQueries.js sendRequestToServer : Ajax query successful");
        currentConsole.log("jsonQueries.js sendRequestToServer : Retrieved data:"+data);
        
        callback(data);
    })
    .error(function(data) {
        currentConsole.log("jsonQueries.js sendRequestToServer : Ajax query error!");
        currentConsole.log("jsonQueries.js sendRequestToServer : failed with status "+data.statusText+"********"+data.status+"********"+data);
    });
//    .success(function(jsonQueryData) { callbackSuccess(jsonQueryData)});
//    .success(function(data) { callbackSuccess(data)})
//    .error(callbackError(data));
}


/********************************************************************
 * Sets up and triggers a call to the Bookmarks-create operation
 */
function triggersBookmarksCreateQuery() {
    
    prepareBookmarksCreateQuery();
    
        
    //    var callbacks={over: overHandler( type );  
    //
    //function overHandler( ) {
    //
    //  return function(type) {
    //
    //      alert(type);
    //
    //  }
    //TODO: doesn't work. I must dig into "closures"
    //http://stackoverflow.com/search?q=%5Bjquery%5D+closure++post

    //    callbackSuccess=handleBookmarksCreateSuccess();
    //    callbackError=handleBookmarksCreateError();
    //    
    sendRequestToServer(onBookmarksCreateQuerySuccess);
}

function onBookmarksCreateQuerySuccess(data) {
    markMyWebLog("Ajax query successful. Created: "+$.parseJSON(data).arFeedbackMsgs[tagData]);
} 

function prepareBookmarksCreateQuery() {
    url="http://localhost/markMyWeb/ajax/bookmarks.php"; 

    if (currentConfigStage=="testing") {
        var urlData="http://www.java-gaming.org/index.php/topic,3685.0.html";
        //    var tagData="programming-games-object-oriented-resources";
        tagData="programming-games-object-oriented-resources";
        var titleData="MVC pattern - Java-Gaming.org";

    }
    else {
       
        tagData=$inputTags.val().split(" ");
        var urlData=$inputUrl.val();
      
        var titleData=$inputTitle.val();
    }
    jsonQueryData={
        "action":"create",
        "url":urlData,
        "title":titleData,
        "tags":tagData
    };    
}
    
function handleBookmarksCreateSuccess(data) {
    currentConsole.log("jsonQueries.js handleBookmarksCreateSuccess : Ajax query successful");
    currentConsole.log("jsonQueries.js handleBookmarksCreateSuccess : Retrieved data:"+data);
}

function handleBookmarksCreateError(data) {
    
}

/********************************************************************************************************************
 * Sets up and triggers a call to the Bookmarks-read tag operation
 */
function triggersBookmarksReadByUrlQuery() {
    prepareBookmarksReadByUrlQuery();
    sendRequestToServer(onBookmarksReadByUrlQuerySuccess);
}

//we create one div for each tag
function onBookmarksReadByUrlQuerySuccess(data) {
//    alert(data);
    var arReturnParameter=$.parseJSON(data).arReturnParameter["default"];
    
    for (var i=0;i<arReturnParameter.length;i++) {
        uiAddTag(arReturnParameter[i].tag);
    }
}

function prepareBookmarksReadByUrlQuery() {
    url="http://localhost/markMyWeb/ajax/bookmarks.php"; 
    
    jsonQueryData={
        "action":"read",
        "fields":"ta",
        "crit":"u",
        "key":currentUrl
    };    
}

/********************************************************************************************************************
 * Sets up and triggers a call to the Bookmarks-delete by URL and Tag operation
 */
function triggersBookmarksDeleteByUrlAndTagQuery(tagToDelete) {    
    prepareBookmarksDeleteByUrlAndTagQuery(tagToDelete);
    
    sendRequestToServer(onBookmarksDeleteByUrlAndTagQuerySuccess);
}

function prepareBookmarksDeleteByUrlAndTagQuery(tagToDelete) {
    url="http://localhost/markMyWeb/ajax/bookmarks.php"; 
    
    jsonQueryData={
        "action":"delete",
        "u":currentUrl,
        "ta":tagToDelete
    };        
}

function onBookmarksDeleteByUrlAndTagQuerySuccess(data) {
    
    var arReturnParameter=$.parseJSON(data).arReturnParameter["default"];
    var tagToDelete=arReturnParameter.tag;
    uiDeleteTag(tagToDelete);
}